﻿#region Copyright © 2011 Prageeth Silva <prageeth@thenewcoders.com>
/*
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the author(s) be held liable for any damages arising from
 * the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 
 *   1. The origin of this software must not be misrepresented; you must not
 *      claim that you wrote the original software. If you use this software
 *      in a product, an acknowledgment in the product documentation would be
 *      appreciated but is not required.
 * 
 *   2. Altered source versions must be plainly marked as such, and must not
 *      be misrepresented as being the original software.
 * 
 *   3. This notice may not be removed or altered from any source distribution.
 */
#endregion

using System.Collections.Generic;
using System.Windows.Forms;

namespace SourcePlus.Ui
{
    public static class UiHelper
    {

        public static bool MoveItemsUp(ListBox list)
        {

            try
            {

                ListBox.SelectedIndexCollection ic = list.SelectedIndices;

                if (ic.Count > 0)
                {

                    // get the selected indices
                    int[] indices = new int[ic.Count];
                    int i = 0;
                    foreach (int idx in ic)
                    {
                        indices[i] = idx;
                        i++;
                    }

                    list.SuspendLayout();

                    // move the selected items
                    List<int> newIndices = new List<int>(indices.Length);
                    foreach (int idx in indices)
                    {
                        int newIdx = idx - 1;
                        if (idx > 0 && !newIndices.Contains(newIdx))
                        {
                            object obj = list.Items[idx];
                            list.Items.RemoveAt(idx);
                            list.Items.Insert(newIdx, obj);
                            newIndices.Add(newIdx);
                        }
                        else
                        {
                            newIndices.Add(idx);
                        }
                    }

                    // reselect the new positions
                    list.SelectedIndices.Clear();
                    foreach (int idx in newIndices)
                    {
                        list.SelectedIndices.Add(idx);
                    }

                    list.ResumeLayout();

                }


            }
            catch
            {
                return false;
            }

            return true;

        }

        public static bool MoveItemsDown(ListBox list)
        {

            try
            {

                ListBox.SelectedIndexCollection ic = list.SelectedIndices;

                if (ic.Count > 0)
                {

                    // get the selected indices
                    int[] indices = new int[ic.Count];
                    int i = ic.Count - 1;
                    foreach (int idx in ic)
                    {
                        indices[i] = idx;
                        i--;
                    }

                    list.SuspendLayout();

                    // move the selected items
                    List<int> newIndices = new List<int>(indices.Length);
                    int max = list.Items.Count - 1;
                    foreach (int idx in indices)
                    {
                        int newIdx = idx + 1;
                        if (idx < max && !newIndices.Contains(newIdx))
                        {

                            object obj = list.Items[idx];
                            list.Items.RemoveAt(idx);
                            list.Items.Insert(newIdx, obj);
                            newIndices.Add(newIdx);
                        }
                        else
                        {
                            newIndices.Add(idx);
                        }
                    }

                    // reselect the new positions
                    list.SelectedIndices.Clear();
                    foreach (int idx in newIndices)
                    {
                        list.SelectedIndices.Add(idx);
                    }

                    list.ResumeLayout();

                }

            }
            catch
            {
                return false;
            }

            return true;

        }


    }
}
